package com.beerq.view.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.beerq.R;
import com.beerq.util.BasicHttp;
import com.beerq.util.Constants;
import com.beerq.util.StringUtil;
import com.beerq.view.BaseActivity;
import com.beerq.view.WebViewActivity;
import com.beerq.view_manager.RegisterAndLoginManager;
import com.beerq.view_manager.ValidateManager;

public class RegisterActivity extends BaseActivity {

    private Button btnOk;
    private EditText mEditTextValidateCode;
    private EditText mEditTextPassword;



    private ValidateManager mValidateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType = 0;
        super.onCreate(savedInstanceState);
        hideTopBar();
        setMainContentView(R.layout.activity_register);

        btnOk = (Button) findViewById(R.id.btn_register);
        mEditTextValidateCode = (EditText) findViewById(R.id.edit_validate_code);
        mEditTextPassword = (EditText) findViewById(R.id.edit_password);

        mValidateManager=new ValidateManager(this);
        mValidateManager.bind();

        setEvent();

    }



    private void setEvent() {

//        //服务条款
        findViewById(R.id.txt_agreement).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(mContext, WebViewActivity.class);
                String url = Constants.domain+"views/agreement.html";
                intent.putExtra("url", url);
                startActivity(intent);

            }
        });


        final TextView txtPhoneNo = (TextView) findViewById(R.id.edit_phone_number);


        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNo = txtPhoneNo.getText().toString();
                if (StringUtil.isEmpty(phoneNo)) {
                    showToast("手机号码不能为空");
                    return;
                } else if (phoneNo.length() != 11) {
                    showToast("手机号码格式不正确");
                    return;
                }

                String validateCode = mEditTextValidateCode.getText().toString();
                if (StringUtil.isEmpty(validateCode)) {
                    showToast("验证码不能为空");
                    return;
                }

                String password = mEditTextPassword.getText().toString();
                if (StringUtil.isEmpty(password)) {
                    showToast("密码不能为空");
                    return;
                }

                if(password.length()<7){
                    showToast("密码位数要大于6位");
                    return;
                }


                if(!mValidateManager.checkValidateCode(validateCode)){
                    return;
                }


                RegisterAndLoginManager.register(mContext, phoneNo, password, "", "", "", new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(String resonseData) {
                        Intent intent = new Intent(mContext, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onFailure(String resonseData) {

                    }
                } );



            }
        });
    }
}
